package com.league.zjxn.jetpack.vmodel

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.SPStaticUtils
import com.league.zjxn.base.BaseViewModel
import com.league.zjxn.entity.HomeSearchResponse
import com.league.zjxn.jetpack.repository.HomeRepository
import com.league.zjxn.utils.JsonUtils
import com.module.lemlin.http.HttpResponseBody
import com.module.lemlin.http.SubscribeViewModel
import java.lang.Exception

const val BUNDLE_KEY_SEARCH = "BUNDLE_KEY_SEARCH"
class SearchViewModel(application: Application) : BaseViewModel(application) {

    private val mHomeRepository: HomeRepository = HomeRepository()

    val mSearchRecordList: MutableList<String>
        get() {
            val search = SPStaticUtils.getString(BUNDLE_KEY_SEARCH)
            return if (search.isNullOrEmpty()) {
                mutableListOf()
            } else try {
                JsonUtils.stringToList(search, String::class.java)
                    .toMutableList()
            } catch (e: Exception) {
                mutableListOf()
            }
        }

    val keywordLiveData = MutableLiveData<String>()

    val searchRecordLiveData = MutableLiveData<MutableList<String>>()
    fun searchRecord(){
        searchRecordLiveData.postValue(mSearchRecordList)
    }


    val homeSearchLiveData = MutableLiveData<HttpResponseBody<HomeSearchResponse>>()
    fun homeSearch(key: String) {
        setRxLifeScope(SubscribeViewModel(homeSearchLiveData)) {
            mHomeRepository.homeSearch(key, page = "1", limit = "50")
        }
    }

}